import { objDeepCopy,getDateString } from "@/assets/js/common.js";

let Controller = {
	created() {
		this.init();
	},
	mounted() {
		
	},
	methods: {
		init() {
			// this.codeEntryAjax(this,this.$route.query.barCodeArr);
			
			this.getReturnReason(this)
		},
		//获取退料原因（表格中）
		getReturnReason(that){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					// url:'/api/base/systemDictionary/getByType/refundMaterialsReason'
					url:apiTest+'/base/systemDictionary/getByType/refundMaterialsReason'
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var arr = [];
						var obj = {};
						var result = data.result;
						if(result){
							
							for (let i = 0; i < result.length; i++) {
								obj = {
									label:result[i].value,
									value:result[i].key
								}
								arr.push(obj);
							}
							that.Model.returnReason = objDeepCopy(arr);
						}
					}else{
			
					}
			
				});
		},
		//扫码入库接口
		codeEntryAjax(that,codeArr){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					// url:'/api/supplier/whWarehouse/materialsScanCode',
					url:apiTest+'/supplier/whWarehouse/materialsScanCode',
					data:{
						barCodes:codeArr,
					}
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result.length == 0){
							this.tips(this,'warning','未找到相关条码');
						}else{
							for (let i = 0; i < result.length; i++) {
								obj = objDeepCopy(result[i]);
								that.Model.tableData.push(obj);
							}
							
						}
					}else{
			
					}
			
				});
		},
		
		//回车或点击确定
		codeEntry(){
			var code = this.Model.searchCode;
			if(code.length == 0){
				this.tips(this,'warning','请输入条码或者商品编码');
				return;
			}
			var tableData = this.Model.tableData;
			var codeIndexOf = this.valueInData(code,tableData);
			if(codeIndexOf == -1){
				this.codeEntryAjax(this,[code]);
			}else{
				this.tips(this,'warning','条码已存在')
			}
			
		},
		//遍历 判断是否存在数组中
		valueInData(val,data){
			var indexof = -1;
			data.forEach(function(value,index,array){
				Object.keys(value).forEach(function(key){
					// console.log(key,value[key]);
					if(key == 'barCode' && value[key] == val){
						indexof = 0;
						return;
					}
				});
			})
			return indexof;
		},
		//删除 表格数据
		deleteTableData(){
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tableData = this.Model.tableData;
			if(TableSelectData.length == 0){
				this.tips(this,'warning','请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				TableSelectData.forEach(function(value,index,array){
					tableData.forEach(function(val,idx,arr){
						if(value == val){
							that.Model.tableData.splice(idx,1);
						}
					})
				})
				this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(() => {
			    
			});
		},
		//确定退料 请求
		confirmReturnAjax(that,params){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					// url:'/api/supplier/whWarehouse/materialsConfirmReturn',
					url:apiTest+'/supplier/whWarehouse/materialsConfirmReturn',
					data:{
						refundMaterialsMap:params
					}
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result){
							that.tips(that, 'success', '操作成功');
							setTimeout(function(){
								that.$router.push({
									path: 'returnListConfirm',
									query: {
										id:result
									}
								})
							},1000)
						}
					}else{
			
					}
			
				});
		},
		//确定退料
		onConfirm(){
			var tableData = this.Model.tableData;
			if (tableData.length == 0) {
				this.tips(this, 'warning', '请选择需要确定收料的数据');
				return;
			} else {
				this.$confirm('此操作将确定收料, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
				}).then(() => {
					var obj = {};
					for (let i = 0; i < tableData.length; i++) {
						obj[tableData[i].id] = tableData[i].select; 
					}
					this.confirmReturnAjax(this, obj)
				}).catch(() => {
			
				});
			}
			
		},
		//取消
		onCancel(){
			
		},
		//打印
		print(){
			
		},
		//返回上一级列表
		back() {
			this.$router.push({
				path: 'returnList'
			})
		},
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			console.log(val);
			this.Model.addGiftsSelectData = val
		  
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = val;
		},
		toSearch(val) {
			console.log(val)
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
		  console.log(val);
		},
		onTabChange(val) {
			console.log(val, this.Model.activeTab)
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
	},
	watch:{
		$route:{
		　　　handler(newValue, oldValue) {
				
		　　　},
		　　　deep: true
		},
  	
	},
	filters:{
		objFilters:function(value){
			
		}
	}
}
export default Controller;
